package org.jactao.rpc;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import io.netty.channel.Channel;
import org.jactao.common.LRequest;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by fanzetao on 2018/4/3.
 */
public class LRequestPool {

    private static int bufferSize = 1024 * 8;

    private static Executor executor = Executors.newFixedThreadPool(500);

    private static LRequestEventFactory factory = new LRequestEventFactory();

    private static Disruptor<LRequestEvent> disruptor = new Disruptor<>(factory, bufferSize, executor);

    private static RingBuffer<LRequestEvent> ringBuffer = disruptor.getRingBuffer();

    public static void publishEvent(LRequest request, Channel channel) {
        long seq = ringBuffer.next();
        LRequestEvent requestEvent = ringBuffer.get(seq);
        requestEvent.setRequest(request);
        requestEvent.setChannel(channel);
        ringBuffer.publish(seq);
    }

    static {
        disruptor.handleEventsWith(new LRequestEventHandler());
        disruptor.start();
    }

}
